using System;
using System.Collections.Generic;
using System.Drawing;
namespace Circus.Syntax
{
	public class SyntaxTree
	{
		internal class PointComparer : IComparer<SyntaxNode>
		{
			public int Compare(SyntaxNode x, SyntaxNode y)
			{
				Point startPoint = x.Range.StartPoint;
				Point startPoint2 = y.Range.StartPoint;
				int num = startPoint.Y - startPoint2.Y;
				if (num == 0)
				{
					num = startPoint.X - startPoint2.X;
				}
				return num;
			}
		}
		private SyntaxNode root;
		private IList<SyntaxNode> stack;
		private IComparer<SyntaxNode> pointComparer;
		public virtual SyntaxNode Root
		{
			get
			{
				return this.root;
			}
			set
			{
				this.root = value;
			}
		}
		public virtual SyntaxNode Current
		{
			get
			{
				return (this.stack.Count > 0) ? this.stack[this.stack.Count - 1] : this.root;
			}
		}
		public SyntaxTree()
		{
			this.stack = new List<SyntaxNode>();
			this.root = new SyntaxNode();
			this.pointComparer = new SyntaxTree.PointComparer();
		}
		public virtual void Clear()
		{
			this.root.Clear();
			this.stack.Clear();
		}
		public virtual void Push(SyntaxNode node)
		{
			this.stack.Add(node);
		}
		public virtual SyntaxNode Pop()
		{
			SyntaxNode syntaxNode = (this.stack.Count > 0) ? this.stack[this.stack.Count - 1] : null;
			if (syntaxNode != null)
			{
				this.stack.RemoveAt(this.stack.Count - 1);
			}
			return syntaxNode;
		}
		public virtual void BlockDeleting(Rectangle rect)
		{
			this.root.BlockDeleting(rect, this.pointComparer);
		}
		public virtual void PositionChanged(int x, int y, int deltaX, int deltaY)
		{
			this.root.PositionChanged(x, y, deltaX, deltaY, this.pointComparer);
		}
		public virtual SyntaxNode FindNode(SyntaxNode obj, IComparer<SyntaxNode> comparer)
		{
			SyntaxNode syntaxNode = this.Root;
			SyntaxNode result = null;
			while (syntaxNode != null)
			{
				result = syntaxNode;
				syntaxNode = syntaxNode.FindNode(obj, comparer);
			}
			return result;
		}
		public virtual void FindNodes(SyntaxNode obj, IComparer<SyntaxNode> comparer, SyntaxNodes nodes)
		{
			if (this.Root != null)
			{
				nodes.Add(this.Root);
				this.Root.FindNodes(obj, comparer, nodes);
			}
		}
		public virtual void Sort(IComparer<SyntaxNode> comparer)
		{
			this.Root.Sort(comparer);
		}
	}
}
